package com.fansea.ai.controller;

import com.fansea.ai.domain.File;
import com.fansea.ai.domain.dto.AjaxResult;
import com.fansea.ai.service.FileService;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Projectname: Spring-AI
 * @Filename: FileController
 * @Author: FANSEA
 * @Date:2025/5/3 13:21
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/file")
public class FileController {

    private final FileService fileService;
    private final VectorStore vectorStore;

    @PostMapping("/upload")
    public AjaxResult uploadFile(MultipartFile file){
        return AjaxResult.success(fileService.uploadDocument(file));
    }

    @PostMapping("/uploadToKnow/{knowledgeId}")
    public AjaxResult uploadFileToKnowledge(MultipartFile file, @PathVariable Long knowledgeId){
        return AjaxResult.success(fileService.uploadToKnowledge(file, knowledgeId));
    }

    @DeleteMapping("/delete/{fileId}")
    public AjaxResult deleteFile(@PathVariable Long fileId){
        File file = fileService.getById(fileId);
        if(file == null){
            return AjaxResult.error("文件不存在");
        }
        String path = file.getPath();
        boolean delete = new java.io.File(path).delete();
        if(delete){
            fileService.removeById(fileId);
            vectorStore.delete("fileId == "+fileId);
            return AjaxResult.success("删除成功");
        }
        return AjaxResult.error("删除失败");
    }

    @PutMapping("/updateStatus/{fileId}")
    public AjaxResult updateFile(Integer status, @PathVariable Long fileId){
        File file = fileService.getById(fileId);
        file.setStatus(status);
        fileService.updateById(file);
        return AjaxResult.success();
    }


}
